package com.geezdata.cps.sdk3rd.wechat;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.sdk3rd.sms.SmsDefaultClient;
import com.geezdata.cps.sdk3rd.wechat.enums.MethodEnum;
import com.geezdata.cps.sdk3rd.wechat.method.Wechat;
import com.geezdata.cps.sdk3rd.wechat.method.WechatAccessToken;
import com.geezdata.cps.sdk3rd.wechat.method.WechatHongbao;
import com.geezdata.cps.sdk3rd.wechat.method.WechatHongbaoRecord;
import com.geezdata.cps.sdk3rd.wechat.method.WechatKefu;
import com.geezdata.cps.sdk3rd.wechat.method.WechatLogin;
import com.geezdata.cps.sdk3rd.wechat.method.WechatMediaUpload;
import com.geezdata.cps.sdk3rd.wechat.method.WechatMiniProgramTemplateMessage;
import com.geezdata.cps.sdk3rd.wechat.method.WechatPayOrder;
import com.geezdata.cps.sdk3rd.wechat.method.WechatPayOrderQuery;
import com.geezdata.cps.sdk3rd.wechat.method.WechatQrCode;
import com.geezdata.cps.sdk3rd.wechat.method.WechatQrCodePage;
import com.geezdata.cps.sdk3rd.wechat.method.WechatShortUrl;
import com.geezdata.cps.sdk3rd.wechat.method.WechatTemplateMessage;
import com.geezdata.cps.sdk3rd.wechat.method.WechatUnionId;
import com.geezdata.cps.sdk3rd.wechat.request.WechatRequest;
import com.geezdata.cps.sdk3rd.wechat.response.WechatResult;

public class WechatDefaultClient implements WechatClient {
	private static final Logger logger = LoggerFactory.getLogger(SmsDefaultClient.class);

	@Override
	public <REQ extends WechatRequest, RES extends WechatResult> RES execute(REQ request) {
		Wechat<REQ, RES> weixin = createWeixin(request.getMethod());
    	if (weixin == null) {
    		logger.error("不支持该微信接口");
    		throw new MyException(ErrorCode.HTTP_EXCEPTION.getCode(), "不支持该微信接口");
    	}
    	
        return weixin.execute(request);
	} 
	
	@SuppressWarnings("unchecked")
	public <REQ extends WechatRequest, RES extends WechatResult> Wechat<REQ, RES> createWeixin(String method) {
		if (StringUtils.isEmpty(method)) {
			return null;
		}
		
		if(method.equals(MethodEnum.LUCKY_MONEY.getName())) {
			return (Wechat<REQ, RES>)(new WechatHongbao());
		} else if(method.equals(MethodEnum.QR_CODE.getName())) {
			return (Wechat<REQ, RES>)(new WechatQrCode());
		} else if(method.equals(MethodEnum.ACCESS_TOKEN.getName())) {
			return (Wechat<REQ, RES>)(new WechatAccessToken());
		} else if(method.equals(MethodEnum.SHORT_URL.getName())) {
			return (Wechat<REQ, RES>) new WechatShortUrl();
		} else if(method.equals(MethodEnum.LUCKY_MONEY_RECORD.getName())) {
			return (Wechat<REQ, RES>) new WechatHongbaoRecord();
		} else if(method.equals(MethodEnum.UNIFIED_ORDER.getName())) {
			return (Wechat<REQ, RES>) new WechatPayOrder();
		} else if(method.equals(MethodEnum.ORDER_QUERY.getName())) {
			return (Wechat<REQ, RES>) new WechatPayOrderQuery();
		} else if(method.equals(MethodEnum.LOGIN.getName())) {
			return (Wechat<REQ, RES>) new WechatLogin();
		} else if(method.equals(MethodEnum.UNION_ID.getName())) {
			return (Wechat<REQ, RES>) new WechatUnionId();
		} else if(method.equals(MethodEnum.QR_CODE_PAGE.getName())) {
			return (Wechat<REQ, RES>) new WechatQrCodePage();
		} else if(method.equals(MethodEnum.KEFU.getName())) {
			return (Wechat<REQ, RES>) new WechatKefu();
		} else if(method.equals(MethodEnum.MEDIA_UPLOAD.getName())) {
			return (Wechat<REQ, RES>) new WechatMediaUpload();
		} else if(method.equals(MethodEnum.MINI_PROGRAM_TEMPLATE_MESSAGE.getName())) {
			return (Wechat<REQ, RES>) new WechatMiniProgramTemplateMessage();
		} else if(method.equals(MethodEnum.WECHAT_TEMPLATE_MESSAGE.getName())) {
			return (Wechat<REQ, RES>) new WechatTemplateMessage();
		}
		
		return null;
	}
}
